<?php
namespace Api\Controller;
use Think\Controller;
class IndexController extends PublicController {
	//***************************
	//  首页数据接口
	//***************************
    public function index(){
        $appid = $_POST['appid'];

    	//如果缓存首页没有数据，那么就读取数据库
    	/***********获取首页顶部轮播图************/
    	$ggtop=M('guanggao')->where("appid ='".$appid."'")->order('sort desc,id asc')->field('id,name,photo')->limit(10)->select();
		foreach ($ggtop as $k => $v) {
			$ggtop[$k]['photo']=__DATAURL__.$v['photo'];
			$ggtop[$k]['name']=urlencode($v['name']);
		}
    	/***********获取首页顶部轮播图 end************/
        $result = M('introduction')->where("del<1 and appid = '".$appid."'")->find();
        $result = str_replace('http://','https://',$result);
        $data['content'] = $result;
        $data['lunbotu'] = $ggtop;
    	echo json_encode($data);
    	exit();
    }
    public function message(){
        $name = $_REQUEST['name'];
        $phone = $_REQUEST['phone'];
        $email = $_REQUEST['email'];
        $content = $_REQUEST['content'];
        $data = array(
            'name' => $name,
            'phone' => $phone,
            'email' => $email,
            'content' => $content,
            'addtime' => time(),
        );
        // print_r($data);die;
        $result = M('message')->add($data);
        if($result){
            echo json_encode(array('code'=>0,'msg'=>'添加成功'));
        }else{
            echo json_encode(array('code'=>-1,'msg'=> '添加失败'));
        }
    }
    //***************************
    //  首页产品 分页
    //***************************
    public function getlist(){
        $page = intval($_REQUEST['page']);
        $limit = intval($page*8)-8;

        $pro_list = M('product')->where('del=0 AND pro_type=1 AND is_down=0 AND type=1')->order('sort desc,id desc')->field('id,name,photo_x,price_yh,shiyong')->limit($limit.',8')->select();
        foreach ($pro_list as $k => $v) {
            $pro_list[$k]['photo_x'] = __DATAURL__.$v['photo_x'];
        }

        echo json_encode(array('prolist'=>$pro_list));
        exit();
    }

    public function ceshi(){
        $str = null;
        $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
        $max = strlen($strPol)-1;

        for($i=0;$i<32;$i++){
            $str.=$strPol[rand(0,$max)];//rand($min,$max)生成介于min和max两个数之间的一个随机整数
        }

        echo $str;
    }

}